package com.wangxiaonan.wxwork.controller;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.wxwork.domain.Agent;
import com.wangxiaonan.wxwork.domain.MessageTemplate;
import com.wangxiaonan.wxwork.domain.ScheduleJob;
import com.wangxiaonan.wxwork.exeption.ServiceException;
import com.wangxiaonan.wxwork.service.AgentService;
import com.wangxiaonan.wxwork.service.MessageTemplateService;
import com.wangxiaonan.wxwork.service.ScheduleService;
import com.wangxiaonan.wxwork.service.TaskService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 定时任务接口
 *
 * @Author: wangxiaonan
 * @Date: 2018/8/30
 **/
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Resource
    private AgentService agentService;
    @Resource
    private ScheduleService scheduleService;
    @Resource
    private MessageTemplateService messageTemplateService;

    @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveSchedule(@RequestParam(value = "agent_id") String agentId, @RequestBody String jsonBody) {
        JSONObject rs = new JSONObject();
        Agent agent = agentService.queryAgentById(agentId);
        if (agent == null) {
            rs.put("status", "agent_not_exists");
            rs.put("msg", "应用不存在");
            return rs.toJSONString();
        }
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setData(jsonBody);
        scheduleJob.setGroup(agentId);

        try {
            scheduleJob = scheduleService.save(scheduleJob);
        } catch (ServiceException e) {
            rs.put("status", "schedule_save_fail");
            rs.put("msg", "定时任务保存失败");
            return rs.toJSONString();
        }

        try {
            scheduleService.scheduleJob(scheduleJob);
        } catch (ServiceException e) {
            rs.put("status", "warn");
            rs.put("msg", "定时任务启动失败");
            return rs.toJSONString();
        }
        rs.put("status", "ok");
        return rs.toJSONString();
    }

    @RequestMapping(value = "/state", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateScheduleStatus(@RequestParam(value = "job_id") Integer jobId,
                                 @RequestParam(value = "option_type") String optionType) {
        JSONObject rs = new JSONObject();
        ScheduleJob scheduleJob = scheduleService.getScheduleJobById(jobId);
        if (scheduleJob == null) {
            rs.put("status", "schedule_not_exists");
            rs.put("msg", "schedule 不存在");
            return rs.toJSONString();
        }

        switch (optionType) {
            case "start":
                scheduleJob.setStatus(ScheduleJob.STATUS_VALID);
                int result = scheduleService.update(scheduleJob);
                if (result <= 0) {
                    rs.put("status", "update_fail");
                    rs.put("msg", "schedule 更新失败");
                    return rs.toJSONString();
                }
                scheduleService.scheduleJob(scheduleJob);
                break;
            case "stop":
                scheduleService.unScheduleJob(scheduleJob);
                scheduleJob.setStatus(ScheduleJob.STATUS_INVALID);
                result = scheduleService.update(scheduleJob);
                if (result <= 0) {
                    rs.put("status", "update_fail");
                    rs.put("msg", "schedule 更新失败");
                    return rs.toJSONString();
                }
                break;
            case "delete":
                scheduleService.unScheduleJob(scheduleJob);
                JSONObject tem = JSONObject.parseObject(scheduleJob.getData());
                result = messageTemplateService.deleteById(tem.getInteger("template_id"));
                if (result > 0) {
                    result = scheduleService.deleteById(scheduleJob.getId());
                }
                if (result <= 0) {
                    rs.put("status", "delete_fail");
                    rs.put("msg", "schedule 删除");
                    return rs.toJSONString();
                }
                break;
            default:
                rs.put("status", "not_support_type");
                rs.put("msg", "不支持的类型");
                return rs.toJSONString();
        }

        rs.put("status", "ok");
        return rs.toJSONString();
    }

    @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateSchedule(@RequestParam(value = "agent_id") String agentId, @RequestBody String jsonBody){
        JSONObject rs = new JSONObject();
        Agent agent = agentService.queryAgentById(agentId);
        if (agent == null) {
            rs.put("status", "agent_not_exists");
            rs.put("msg", "应用不存在");
            return rs.toJSONString();
        }
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setData(jsonBody);
        scheduleJob.setGroup(agentId);
        try {
            scheduleJob = scheduleService.updateSchedule(scheduleJob);
        } catch (ServiceException e) {
            rs.put("status", "schedule_update_fail");
            rs.put("msg", "数据更新失败");
            return rs.toJSONString();
        }

        try{
            scheduleService.scheduleJob(scheduleJob);
        } catch (ServiceException e) {
            rs.put("status", "schedule_update_fail");
            rs.put("msg", "定时任务更新失败");
            return rs.toJSONString();
        }

        rs.put("status","ok");
        return rs.toJSONString();
    }

}
